
const sequelize = require('../shared/sequelize')
// const Discuss = sequelize.import('./model/discuss')
const Client = require('../model/client')(sequelize,sequelize.DataTypes)


const md5 = require('js-md5')
//用户登陆
exports.clientLogin = async function(username,password){
    //   const {rows,count} = await Client.findAndCountAll()
    // const colList = rows.map(row=>row.toJSON())
    var pwd = md5(password)
    await Client.findOne({where:{username:username}}).then(result =>{
        if(result && pwd == result.password){
            // 保存session
            console.log(username,password)
            return {code:'200',msg:'success'}
        }else{
            return {code:'400',msg:'账号或密码错误'}
        }
    })
    
    // return {list:colList,count:count}
}

//用户注册
exports.clientRegister = async function(username,password){
    
    // Collection.
    // 验证账号是否重复
    let result = await Client.findOne({where:{username:username}})
    console.log('result',result)
    if(result){
        console.log(400)
        return {'code':400,'msg':'账号重复'}
    }
    console.log(200)
    let pwd = md5(password)
    console.log(username,pwd)
    Client.create({username:username,password:pwd})
    return {'code':200,'msg':'注册成功'}
}

// 查询用户信息
exports.clientView = async function(username){
    let result = await Client.findOne({where:{username:username}})
    return result
    // return usertoken  
}